package com.tcm.ucenter.web.controller;

import com.base.core.utils.R;
import com.tcm.ucenter.domain.dto.UserAccountDTO;
import com.tcm.ucenter.domain.enums.AccountTypeEnum;
import com.tcm.ucenter.service.UserAccountFactory;
import com.tcm.ucenter.service.UserAccountService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/ucenter")
@RestController
public class UserAccountController {

    @DubboReference(version = "0.0.1-alpha", timeout = 6000, check = false, retries = 3)
    private UserAccountFactory userAccountFactory;

    @DubboReference(version = "0.0.1-alpha", timeout = 6000, check = false, retries = 3)
    private UserAccountService userAccountService;


    @GetMapping("/account/view")
    public R view(@RequestParam("userId") Long userId) {
        return R.success(userAccountService.findByUserId(userId));
    }

    @PostMapping("/account/save")
    public R create(@RequestBody UserAccountDTO userAccountDto) {
        try{
            userAccountFactory.account(AccountTypeEnum.BALANCE.value()).change(userAccountDto.getUserId(),userAccountDto.getBalance(),"测试账户");
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
        return R.success();
    }

}

